package com.tgy.leetcode;

import java.util.*;

/**
 *
 * https://leetcode-cn.com/problems/flatten-nested-list-iterator/
 *
 */


class NestedIterator01 implements Iterator<Integer> {

   private Deque<Iterator<NestedInteger>> deque = new LinkedList<>();

  public NestedIterator01(List<NestedInteger> nestedList) {

      deque.add(nestedList.iterator());
  }

  @Override
  public Integer next() {

      return deque.getLast().next().getInteger();
  }

  @Override
  public boolean hasNext() {

      while (!deque.isEmpty()) {

          if (!deque.getLast().hasNext()) {

              deque.removeLast();
              continue;
          }

          NestedInteger nestedInteger = deque.getLast().next();
          if (nestedInteger.isInteger()) {

              ArrayList<NestedInteger> arrayList = new ArrayList<>();
              arrayList.add(nestedInteger);
              deque.addLast(arrayList.iterator());
              return true;
          }else {
              deque.addLast(nestedInteger.getList().iterator());
          }
      }

     return false;
  }


}

public class _341_扁平化嵌套列表迭代器_01 {


}
